/config,nproc,8
/config,nres,20000
/FILNAME, FSW_1018_pin
/PREP7
/title, sequentially coupled analysis with heat flux on top surface
Antype,trans

ET,1,SOLID70
MP,DENS,1,2700
MPTEMP,,20,100,150,200,250,300,400,500,600,700,750,800,850,900
MPDATA,KXX,1,,52,51.5,50.5,49.5,48,47,42,38,34,31,29,26.5,26,26.5  
MPDATA,C,1,,480,490,500,515,530,550,600,670,750,800,840,900,970,1050  


length=0.203
tlength=0.175
width=0.101
thick=0.00635
xstart=0.014
nthke=4
smesize=0.001537878

ncopy=NINT(length/(4*smesize))
block,,4*smesize,,thick,,10*smesize
block,,4*smesize,,thick,10*smesize,12*smesize
block,,4*smesize,,thick,12*smesize,14*smesize
block,,4*smesize,,thick,14*smesize,width
nummrg,all
numcmp,all
lesize,1,,,nthke
MSHAPE,0,3D
MSHKEY,1
esize,smesize
vMESH,1
lesize,15,,,2,
lesize,13,,,2,
lesize,14,,,4,
lesize,16,,,4,
lesize,17,,,1,
lesize,20,,,1,
lesize,18,,,1,
lesize,19,,,1,
vMESH,2
lesize,22,,,2,
lesize,24,,,2,
lesize,21,,,2,
lesize,23,,,2,
lesize,25,,,1,
lesize,26,,,1,
lesize,27,,,1,
lesize,28,,,1,
vmesh,3
lesize,36,,,18
vmesh,4
VGEN,ncopy, all,,,4*smesize,,,,,
nummrg,all
FINISH

/SOLU 
tunif,25 
allsel
nsel,s,loc,y,
SF,all,CONV,250,20 
allsel
nsel,s,loc,x,0
nsel,a,loc,x,length
nsel,a,loc,z,width
nsel,a,loc,y,thick
SF,all,CONV,30,20
allsel


kbc,1 
time,0.001
DELTIM,0.001 
solve

AUTOTS,ON
ddept=0.00622
ddtime=0.5
dvel=0.00622/175
dinc=ddtime*dvel
dtime=0.2
dtotal=178
dweltime=2
ndwel=dweltime/dtime
dnloct=NINT(dtotal/ddtime) 
tvel=0.00042                       !welding speed
xinc=dtime*tvel
nloct=NINT(tlength/xinc)
rshoulder=0.0095
rprobe=0.00395
phinput=291.07
nheatgen=phinput/(dnloct*dinc*3.14*rprobe*rprobe)
ehfulx=654.92

    nsel,s,loc,y,thick
    nsel,r,loc,z,-rprobe,rprobe
    nsel,r,loc,x,-rprobe,rprobe
    ESLN,s,
    SFEDELE,all,4,CONV
allsel
    nsel,s,loc,z,-(rshoulder+smesize),(rshoulder+smesize)
    *get,numnode,node,0,count   !number of nodes in the selected set
    *get,lnnum,node,0,num,min    !loest node number in the selected set
    *dim,nodenumber,ARRAY,numnode 
    *dim,nodenumber2,ARRAY,numnode 
    *dim,nodenumber3,ARRAY,numnode 
    *dim,nodehflux,ARRAY,numnode
    *dim,emnumber,ARRAY,numnode 
    allsel
! plunge
*do,ka,1,dnloct,1
    x0=xstart   !start point
    z0=0
    nsel,s,loc,z,-(rshoulder+smesize),(rshoulder+smesize)

     nnum=lnnum
     kd=0
    *do,kb,1,numnode
       *get,xlocation,node,nnum,loc,x
       *get,zlocation,node,nnum,loc,z
       *get,ylocation,node,nnum,loc,y
        rd=SQRT((xlocation-x0)**2+(zlocation-z0)**2)

          *if,rd,le,rprobe,THEN
            *if,ylocation,ge,(thick-ka*dinc),then
	            kd=kd+1
	            nodenumber2(kd)=nnum
 	            bf,nnum,HGEN,nheatgen
             *endif
          *endif
       *get,nexthn,node,nnum,nxth
        nnum=nexthn
     *enddo
    allsel
        step2time=ka*ddtime+0.001
        time,step2time
        DELTIM,ddtime
        KBC,1 
        outres,NSOL,LAST       
        solve
/solu
    *do,ke,1,kd
     bfdele,nodenumber2(ke),HGEN
    *enddo
*enddo
allsel



*do,i,1,(nloct+ndwel),1
        *if,i,le,ndwel,then
           x0=xstart
        *else
          x0=xstart+(i-ndwel)*xinc   !start point
        *endif
           z0=0
    nsel,s,loc,z,-(rshoulder+smesize),(rshoulder+smesize)

     nnum=lnnum
     jj=0
     kk=0
     tt=0
    *do,ia,1,numnode
       *get,xlocation,node,nnum,loc,x
       *get,zlocation,node,nnum,loc,z
       *get,ylocation,node,nnum,loc,y
        rd=SQRT((xlocation-x0)**2+(zlocation-z0)**2)

          *if,rd,le,rshoulder,and,rd,gt,rprobe,THEN
            *if,ylocation,eq,thick,then
            jj=jj+1
            nodenumber(jj)=nnum
            nodehflux(jj)=3*ehfulx*rd/(2*3.1416*(rshoulder**3-rprobe**3))
            *endif
          *endif
          *if,rd,le,rshoulder,THEN
            *if,ylocation,eq,thick,then
            tt=tt+1
            nodenumber3(tt)=nnum
            *endif
          *endif
          *if,rd,le,rprobe,then
            kk=kk+1
            nodenumber2(kk)=nnum
             bf,nnum,HGEN,nheatgen
          *endif
       *get,nexthn,node,nnum,nxth
        nnum=nexthn
     *enddo
    allsel
     *do,ta,1,tt,1
        *if,ta,eq,1,then
          nsel,s,node,,nodenumber3(ta) 
        *else
         nsel,a,node,,nodenumber3(ta)
        *endif
     *enddo
     ESLN,s,
     SFEDELE,all,4,CONV
    allsel

     *do,ib,1,jj,1
        *if,ib,eq,1,then
          nsel,s,node,,nodenumber(ib) 
        *else
         nsel,a,node,,nodenumber(ib)
         n3=nodenumber(ib)
        *endif
     *enddo
     ESLN,s,

     *get,numelem,elem,0,count
     *get,lenum,elem,0,num,min

     enum=lenum
     nsle,all    
    *do,ii,1,numelem
     emnumber(ii)=enum
     nnumk=nelem(enum,3) 
     nnuml=nelem(enum,4) 
     nnumo=nelem(enum,7) 
     nnump=nelem(enum,8) 
     hfluxl=0
     hfluxk=0
     hfluxp=0
     hfluxo=0
       *do,iii,1,jj
          *if,nnuml,eq,nodenumber(iii),then
              hfluxl=nodehflux(iii)
             *elseif,nnumk,eq,nodenumber(iii)
              hfluxk=nodehflux(iii)
             *elseif,nnump,eq,nodenumber(iii)
              hfluxp=nodehflux(iii)
             *elseif,nnumo,eq,nodenumber(iii)
              hfluxo=nodehflux(iii)
          *endif
       *enddo
       aehflux=(hfluxl+hfluxk+hfluxp+hfluxo)/4
       sfe,enum,4,HFLUX,,hfluxk,hfluxl,hfluxp,hfluxo 

      *get,nexthe,elem,enum,nxth
      enum=nexthe
   *enddo
  allsel
  step3time=step2time+i*dtime          
        time,step3time
        DELTIM,dtime
        KBC,1
outres,erase 
        outres,basic,LAST        
        solve
/solu
    *do,id,1,numelem
     enum=emnumber(id)
     SFEDELE,enum,4,hflux
    *enddo
    *do,ik,1,kk
     bfdele,nodenumber2(ik),HGEN
    *enddo
    nsel,s,loc,y,thick
    SF,all,CONV,30,20
    allsel
*enddo


*do,hhkk,1,30
step5time=hhkk*0.5+step3time
        time,step5time
        DELTIM,0.5
outres,erase 
        outres,basic,LAST
solve 
*enddo
finish

finish
/PREP7
Antype,trans
ET,1,SOLID45
mp,ex,1, 200e9        
mp,alpx,1,0.000012
mp,nuxy,1,.29
MP,DENS,1,7870
!   
TB,BKIN,1,5               ! Activate a data table
TBTEMP,25                ! Temperature = 25
TBDATA,1,262E6,0           ! Yield = 44,000; Tangent modulus = 0
TBTEMP,300                
TBDATA,1,260E6,0
! TBTEMP,400                
! TBDATA,1,250E6,0 
! TBTEMP,500                
! TBDATA,1,200E6,0
! TBTEMP,600                
! TBDATA,1,110E6,0
! TBTEMP,650                
! TBDATA,1,75E6,0 
! TBTEMP,700                
! TBDATA,1,50E6,0
TBTEMP,800                
TBDATA,1,30E6,0
TBTEMP,900                
TBDATA,1,15E6,0   
!   
nsel,all
nsel,s,loc,z,width
nsel,a,loc,z,-width
nsel,r,loc,y,0
d,all,ux,0
d,all,uy,0
d,all,uz,0
nsel,all
nsel,s,loc,z,0
dsym,symm,z
allsel 


finish
/solu
NLGEOM,off
AUTOTS,off
tref,25
ldread,temp,1,,,,,rth  ! Read in temperatures from thermal run
time,0.001
outres,erase
OUTRES,BASIC,last   
solve
/solu
*do,kkjj,2,(dnloct+1),1
ldread,temp,kkjj,,,,,rth  ! Read in temperatures from thermal run
steptime=(kkjj-1)*ddtime+0.001
time, steptime
DELTIM,ddtime/5,ddtime/10000,ddtime
outres,erase
OUTRES,BASIC,last
kbc,0
solve
/solu 
*enddo
hdnloct=nloct+ndwel-20
*do,kc,1,hdnloct,1
kd=kkjj+kc
ldread,temp,kd,,,,,rth  ! Read in temperatures from thermal run
steptime=dtime+steptime
time, steptime
DELTIM,dtime/20,dtime/10000,dtime
outres,erase
OUTRES,BASIC,last
kbc,0
solve 
*enddo
*do,kke,1,20,1
kkf=kke+kd
ldread,temp,kkf,,,,,rth  ! Read in temperatures from thermal run
steptime=ddtime+steptime
time, steptime
DELTIM,dtime/50,dtime/10000,ddtime
outres,erase
OUTRES,BASIC,last
kbc,0
solve 
*enddo



nsel,all
nsel,s,loc,z,width
nsel,a,loc,z,-width
nsel,r,loc,y,0
DDELE,all,all
allsel
nsel,s,loc,x,0
nsel,r,loc,y,thick
nsel,r,loc,z,width
d,all,all,0
allsel
*do,hhii,1,30
step5time=0.5*hhii+steptime
kh=kkf+hhii
ldread,temp,kh,,,,,rth  ! Read in temperatures from thermal run
        time,step5time
        DELTIM,0.02,0.001,5
outres,erase
OUTRES,BASIC,last
        kbc,0
        solve
	/solu
*enddo
finish
save